package org.tgcloud.group.core.date;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: zhy
 * @create: 2023-08-01 17:14
 * @description: 节假日工具
 */
public class HolidayUtils {

    /**
     * java获取国家法定节假日和周末
     * @param year /
     * @param month /
     * @return /
     */
    public static Set<String> JJR(int year, int month) throws Exception{
        //获取所有的周末
        Set<String> monthWekDay = getMonthWekDay(year, month);
        //http://timor.tech/api/holiday api文档地址
        Map jjr = getJjr(year, month + 1);
        Integer code = (Integer) jjr.get("code");
        if (code != 0) {
            return monthWekDay;
        }
        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.get("holiday");
        Set<String> strings = holiday.keySet();
        for (String str : strings) {
            Map<String, Object> stringObjectMap = holiday.get(str);
            Integer wage = (Integer) stringObjectMap.get("wage");
            String date = (String) stringObjectMap.get("date");
            //筛选掉补班
            if (wage.equals(1)) {
                monthWekDay.remove(date);
            } else {
                monthWekDay.add(date);
            }
        }
        return monthWekDay;
    }

    /**
     * 获取节假日不含周末
     * @param year /
     * @param month /
     * @return /
     */
    public static LinkedHashMap getJjr(int year, int month) throws Exception{

        URL url = new URL("https://timor.tech/api/holiday/year/" + year);
        String msg = null;
        try {
            HttpURLConnection connection = ((HttpURLConnection)url.openConnection());
            connection.addRequestProperty("User-Agent", "Mozilla/4.0");
            InputStream input;
            if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works
                input = connection.getInputStream();
            else input = connection.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            msg =reader.readLine();
            System.out.println(msg);
        } catch (IOException e) {
            System.err.println(e);
        }
        return JSONObject.parseObject(msg, LinkedHashMap.class);

//        String url = "http://timor.tech/api/holiday/year/" + year;
//        OkHttpClient client = new OkHttpClient();
//        Response response;
//        //解密数据
//        String rsa = null;
//        Request request = new Request.Builder()
//                .url(url)
//                .get()
//                .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                .build();
//        try {
//            response = client.newCall(request).execute();
//            rsa = response.body().string();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return JSONObject.parseObject(rsa, LinkedHashMap.class);
    }

    /**
     * 获取周末  月从0开始
     * @param year /
     * @param month /
     * @return /
     */
    public static Set<String> getMonthWekDay(int year, int month) {
        Set<String> dateList= new LinkedHashSet<>();
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, month, 1);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(simdf.format(calendar.getTime()));
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                dateList.add(simdf.format(calendar.getTime()));
            }
        }
        return dateList;
    }


    public static LinkedHashMap getAllDay(int year, int month) throws Exception{
        URL url = new URL("https://tool.bitefu.net/jiari/?info=2&d=" + year + (month < 10 ? ( "0" + month ) : month));
        String msg = null;
        try {
            HttpURLConnection connection = ((HttpURLConnection)url.openConnection());
            connection.addRequestProperty("User-Agent", "Mozilla/4.0");
            InputStream input;
            if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works
                input = connection.getInputStream();
            else input = connection.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            msg =reader.readLine();
            System.out.println(msg);
        } catch (IOException e) {
            System.err.println(e);
        }
        return JSONObject.parseObject(msg, LinkedHashMap.class);
    }


    /**
     * 获取节假日不含周末
     * @param year /
     * @param month /
     * @return /
     */
    public static LinkedHashMap getJjrByYearMonth(int year, Integer month) throws Exception{
        String monthString = month < 10 ? ("0" + month) : month.toString();
        URL url = new URL("https://timor.tech/api/holiday/year/" + year + "-" + monthString);
        String msg = null;
        try {
            HttpURLConnection connection = ((HttpURLConnection)url.openConnection());
            connection.addRequestProperty("User-Agent", "Mozilla/4.0");
            InputStream input;
            if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works
                input = connection.getInputStream();
            else input = connection.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            msg =reader.readLine();
        } catch (IOException e) {
            System.err.println(e);
        }
        return JSONObject.parseObject(msg, LinkedHashMap.class);

    }



}
